package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageResponse;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * @Description NursingProjectServiceImpl
 * @Author luohai
 * @Date 2024-10-15
 */
@Service
public class NursingProjectServiceImpl implements NursingProjectService {
    @Autowired
    private NursingProjectMapper nursingProjectMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    /**
     * 护理项目分页查询
     * @param name
     * @param pageNum
     * @param pageSize
     * @param status
     * @return
     */
    @Override
    public PageResponse<NursingProjectVo> nursingPaginationQuery(String name, Integer pageNum, Integer pageSize, Integer status) {
        //生成key值
        String key="nursingProject::"+(name==null?"":name)+(status==null?"":status)+pageSize+pageNum;
        //根据key查询缓存中是否存在
        String values = stringRedisTemplate.opsForValue().get(key);
        //不存在就将查询到的数据添加到缓存
        if (StrUtil.isEmpty(values)){
            PageHelper.startPage(pageNum,pageSize);
            Page<NursingProjectVo> page=nursingProjectMapper.nursingPaginationQuery(name,status);
            PageResponse<NursingProjectVo> objectPageResponse = new PageResponse<>(page);
            objectPageResponse.setRecords(page.getResult());
            stringRedisTemplate.opsForValue().set(key, JSONUtil.toJsonStr(objectPageResponse),30, TimeUnit.MINUTES);
            return objectPageResponse;
        }else {
            //不为空直接返回缓存内的已有的数据
            return JSONUtil.toBean(values,PageResponse.class);
        }
    }

    /**
     * 新增护理项目
     * @param nursingProjectDto
     */
    @Override
    public void addNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.addNursingProject(bean);
        //修改数据后要删除缓存内的数据，保持数据一致性
        deleteRedisValues("nursingProject::*");
    }
    //删除redis内所有缓存的数据
    private void deleteRedisValues(String key){
        //获取缓存内的所有key值
        Set<String> keys = stringRedisTemplate.keys(key);
        //根据key删除缓存，参数可以是set集合
        stringRedisTemplate.delete(keys);
    }

    /**
     * 根据id查询护理项目（数据回显）
     * @param id
     * @return
     */
    @Override
    public NursingProjectVo searchNursingProjectById(Long id) {
        return nursingProjectMapper.searchNursingProjectById(id);
    }

    /**
     * 修改护理项目
     * @param nursingProjectDto
     * @return
     */
    @Override
    public void modifyNursingProject(NursingProjectDto nursingProjectDto) {
        NursingProject bean = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProjectMapper.modifyNursingProject(bean);
        deleteRedisValues("nursingProject::*");
    }

    /**
     * 护理项目启禁用
     * @param id
     * @param status
     * @return
     */
    @Override
    public void startAndBan(Long id, Integer status) {
        nursingProjectMapper.startAndBan(id,status);
        deleteRedisValues("nursingProject::*");
    }

    /**
     * 根据id删除护理项目
     * @param id
     * @return
     */
    @Override
    public void deleteNursingProject(Long id) {
        nursingProjectMapper.deleteNursingProject(id);
        deleteRedisValues("nursingProject::*");
    }

    /**
     * 查询所有护理项目
     * @return
     */
    @Override
    public List<NursingProjectVo> searchAll() {
        return nursingProjectMapper.searchAll();
    }
}
